home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / game / shoot / athrust.lha / AmigaThrust / src / hiscore.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-05  |  2.0 KB  |  122 lines

  1.  
  2. /* Written by Peter Ekberg, peda@lysator.liu.se */
  3.  
  4. #ifdef HAVE_CONFIG_H
  5. #include "config.h"
  6. #endif
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12.  
  13. #include "thrust_t.h"
  14. #include "hiscore.h"
  15.  
  16. highscoreentry highscorelist[HIGHSCORES];
  17.  
  18. void
  19. writehighscores(void)
  20. {
  21.   FILE *fp;
  22.   int i;
  23.  
  24.   fp=fopen(HIGHSCOREFILE, "wb");
  25.   if(fp==NULL)
  26.     return;
  27.   for(i=0; i<HIGHSCORES; i++) {
  28.     fwrite(highscorelist[i].name, 40, 1, fp);
  29.     fputc(highscorelist[i].score    , fp);
  30.     fputc(highscorelist[i].score>>8 , fp);
  31.     fputc(highscorelist[i].score>>16, fp);
  32.     fputc(highscorelist[i].score>>24, fp);
  33.   }
  34.   fclose(fp);
  35. }
  36.  
  37. int
  38. readhighscores(void)
  39. {
  40.   FILE *fp;
  41.   int res;
  42.   int i, j;
  43.  
  44.   fp=fopen(HIGHSCOREFILE, "rb");
  45.   if(fp==NULL)
  46.     return(0);
  47.  
  48.   for(i=0; i<HIGHSCORES; i++) {
  49.     res = fread(highscorelist[i].name, 40, 1, fp);
  50.     if(res != 1)
  51.       return(0);
  52.     highscorelist[i].score = 0;
  53.     for(j=0; j<4; j++) {
  54.       res = fgetc(fp);
  55.       if(res == EOF)
  56.     return(0);
  57.       highscorelist[i].score += res<<(8*j);
  58.     }
  59.   }
  60.  
  61.   fclose(fp);
  62.   return(1);
  63. }
  64.  
  65. char *
  66. standardname(void)
  67. {
  68.   char *tmp;
  69.   static char name[40];
  70.  
  71.   tmp=getenv("USER");
  72.   if(tmp==NULL)
  73.     tmp=getenv("LOGNAME");
  74.   if(tmp==NULL)
  75.     name[0]=0;
  76.   else {
  77.     strncpy(name, tmp, 39);
  78.     name[39]=0;
  79.     name[0]=toupper(name[0]);
  80.   }
  81.  
  82.   return(name);
  83. }
  84.  
  85. int
  86. inithighscorelist(void)
  87. {
  88.   int i;
  89.   
  90.   if(!readhighscores()) {
  91.     for(i=0; i<HIGHSCORES; i++) {
  92.       strcpy(highscorelist[i].name, "John Doe");
  93.       highscorelist[i].score=(5-i)*1000;
  94.     }
  95.   }
  96.   
  97.   return(0);
  98. }
  99.  
  100. int
  101. ahighscore(int score)
  102. {
  103.   return(score>highscorelist[HIGHSCORES-1].score);
  104. }
  105.  
  106. void
  107. inserthighscore(char *name, int score)
  108. {
  109.   int i;
  110.  
  111.   for(i=HIGHSCORES; i>0 && score>highscorelist[i-1].score; i--) {
  112.     if(i<HIGHSCORES) {
  113.       strcpy(highscorelist[i].name, highscorelist[i-1].name);
  114.       highscorelist[i].score = highscorelist[i-1].score;
  115.     }
  116.   }
  117.   if(i<HIGHSCORES) {
  118.     strcpy(highscorelist[i].name, name);
  119.     highscorelist[i].score = score;
  120.   }
  121. }
  122.